package com.tancire.question.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.utils.DateUtils;
import com.tancire.question.domain.QuestionCategory;
import com.tancire.question.domain.QuestionTree;
import com.tancire.question.service.IQuestionCategoryService;
import org.springframework.stereotype.Service;
import com.tancire.question.mapper.QuestionSectorMapper;
import com.tancire.question.domain.QuestionSector;
import com.tancire.question.service.IQuestionSectorService;

/**
 * 面试岗位Service业务层处理
 *
 * @author tancire
 * @date 2024-06-07
 */
@Service
public class QuestionSectorServiceImpl implements IQuestionSectorService {

    @Resource
    private QuestionSectorMapper questionSectorMapper;

    @Resource
    private IQuestionCategoryService questionCategoryService;

    /**
     * 查询面试岗位
     *
     * @param sectorId 面试岗位主键
     * @return 面试岗位
     */
    @Override
    public QuestionSector selectQuestionSectorBySectorId(Long sectorId) {
        return questionSectorMapper.selectQuestionSectorBySectorId(sectorId);
    }

    /**
     * 查询面试岗位列表
     *
     * @param questionSector 面试岗位
     * @return 面试岗位
     */
    @Override
    public List<QuestionSector> selectQuestionSectorList(QuestionSector questionSector) {
        return questionSectorMapper.selectQuestionSectorList(questionSector);
    }

    /**
     * 新增面试岗位
     *
     * @param questionSector 面试岗位
     * @return 结果
     */
    @Override
    public int insertQuestionSector(QuestionSector questionSector) {
        questionSector.setCreateTime(DateUtils.getNowDate());
        return questionSectorMapper.insertQuestionSector(questionSector);
    }

    /**
     * 修改面试岗位
     *
     * @param questionSector 面试岗位
     * @return 结果
     */
    @Override
    public int updateQuestionSector(QuestionSector questionSector) {
        questionSector.setUpdateTime(DateUtils.getNowDate());
        return questionSectorMapper.updateQuestionSector(questionSector);
    }

    /**
     * 批量删除面试岗位
     *
     * @param sectorIds 需要删除的面试岗位主键
     * @return 结果
     */
    @Override
    public int deleteQuestionSectorBySectorIds(Long[] sectorIds) {
        return questionSectorMapper.deleteQuestionSectorBySectorIds(sectorIds);
    }

    /**
     * 删除面试岗位信息
     *
     * @param sectorId 面试岗位主键
     * @return 结果
     */
    @Override
    public int deleteQuestionSectorBySectorId(Long sectorId) {
        return questionSectorMapper.deleteQuestionSectorBySectorId(sectorId);
    }

    /**
     * 查询行业分类树
     *
     * @return
     */
    @Override
    public List<QuestionTree> selectQuestionSectorTree() {
        // 获取所有行业
        List<QuestionSector> questionSectorList = this.questionSectorMapper.selectQuestionSectorList(new QuestionSector());
        // 再查询所有子分类
        List<QuestionCategory> questionCategoryList = this.questionCategoryService.selectQuestionCategoryList(new QuestionCategory());
        // 循环处理数据
        List<QuestionTree> tree = new ArrayList<>();
        for (QuestionSector sector : questionSectorList) {
            QuestionTree node = new QuestionTree();
            node.setId(sector.getSectorId());
            node.setLabel(sector.getSectorName());
            // 筛选子节点
            List<QuestionCategory> collect = questionCategoryList.stream()
                    .filter(x -> Objects.equals(x.getSectorId(), sector.getSectorId())).collect(Collectors.toList());

            // 处理子节点
            List<QuestionTree> children = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(collect)) {
                for (QuestionCategory category : collect) {
                    QuestionTree child = new QuestionTree();
                    child.setId(category.getCategoryId());
                    child.setLabel(category.getCategoryName());
                    children.add(child);
                }
            }
            // 保存节点
            node.setChildren(children);
            tree.add(node);
        }
        return tree;
    }
}
